<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="dialogVisible=true" type="primary" style="float:right;margin-top:13px;">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.license"
                      :disabled="saveVehicleForm.id!=null"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.code"
                      :disabled="saveVehicleForm.id!=null"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="saveVehicleForm.displacement">
              <el-option label="1.6" value="1"></el-option>
              <el-option label="2.5" value="2"></el-option>
              <el-option label="4" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.type">
              <!--字典应用-->
              <el-option v-for="item in vehicleTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="轿车" value="10"></el-option>
              <el-option label="客车" value="20"></el-option>
              <el-option label="货车" value="30"></el-option>
              <el-option label="挂车" value="40"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="saveVehicleForm.color">
              <el-option v-for="item in vehicleColorArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveVehicleForm.buyTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveVehicleForm.regTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.batteryType">
              <el-option v-for="item in batteryTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="铅酸蓄电池" value="10"></el-option>
              <el-option label="镍氢电池" value="20"></el-option>
              <el-option label="钠硫电池" value="30"></el-option>
              <el-option label="二次锂电池" value="40"></el-option>
              <el-option label="空气电池" value="50"></el-option>
              <el-option label="三元锂电池" value="60"></el-option>
              <el-option label="碱性燃料电池" value="70"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.brand"
                  @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="searchVehicleForm.license"
                  @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormat"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center" :formatter="batteryTypeFormat"/>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="statusTypeFormat"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="danger" size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');

// 定义对象用于保存车辆搜索条件
const searchVehicleForm = ref({
  brand: '',
  license: ''
});
// 定义数组用于保存车辆表格数据
const vehicleArr = ref([])
// 定义加载车辆的方法
const loadVehicle = () => {
  let data = qs.stringify(searchVehicleForm.value)
  console.log(data);
  axios.get(BASE_URL + '/v1/vehicle/select?' + data)
      .then((response) => {
        if (response.data.code === 2000) {
          vehicleArr.value = response.data.data;
          console.log(vehicleArr.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}

// 定义变量用于保存车辆类型数据（来自字典）
const vehicleTypeArr = ref([]);
// 定义变量用于保存电池类型数据（来自字典）
const batteryTypeArr = ref([]);
// 定义变量用于保存车辆颜色数据（来自字典）
const vehicleColorArr = ref([]);

// 进入页面立即调用的方法
onMounted(() => {
  loadVehicle();  // 调用加载车辆方法
  loadDictOptions(vehicleTypeArr, 'vehicle_type');
  loadDictOptions(batteryTypeArr, 'battery_type');
  loadDictOptions(vehicleColorArr, 'vehicle_color');
})

// 重置搜索条件
const resetSearch = () => {
  searchVehicleForm.value = {};
  loadVehicle();
}

// 定义变量，用于保存新增车辆信息
const saveVehicleForm = ref({
  brand: '',
  license: '',
  model: '',
  code: '',
  displacement: '',
  type: '',
  color: '',
  kilometers: '',
  buyTime: '',
  regTime: '',
  price: '',
  batteryType: '',
});

// 定义方法校验上牌时间和购买时间先后顺序的问题（其实这个验证没有意义，因为实际上你可能卖到二手车）
const validateDate = () => {
  const buyTime = saveVehicleForm.value.buyTime;
  const regTime = saveVehicleForm.value.regTime;
  if (buyTime && regTime) {  // 如果两个时间都有值，才进行比较
    const buyDate = new Date(buyTime);
    const regDate = new Date(regTime);
    if (buyDate > regDate) {
      ElMessage.error('购买时间不能早于上牌时间！');
      saveVehicleForm.value.regTime='';
    }
  }
}

// 定义保存车辆信息方法
const saveVehicle = () => {
  // 加点前端验证
  if(!saveVehicleForm.value.license || saveVehicleForm.value.license.trim()==''){
    ElMessage.error('请输入车牌号！');
    return;
  }
  if(!saveVehicleForm.value.code || saveVehicleForm.value.code.trim()==''){
    ElMessage.error('请输入车辆识别码！');
    return;
  }
  if(!saveVehicleForm.value.buyTime){
    ElMessage.error('请输入购买时间！');
    return;
  }

  let data = saveVehicleForm.value
  console.log(data);
  axios.post(BASE_URL + '/v1/vehicle/save', data)
      .then((response) => {
        if (response.data.code === 2000) {
          ElMessage.success('保存成功！');
          dialogVisible.value = false;
          saveVehicleForm.value = {};  // 清空表单数据
          loadVehicle();
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

// 定义转换车辆类型显示为文字的方法：10 轿车，20 客车，30 货车，40 挂车
const vehicleTypeFormat = (row, column, cellValue, index) => {
  if(!vehicleTypeArr.value){
    return cellValue;
  }
  for(let i of vehicleTypeArr.value){
    if(i.value==row.type){
      cellValue = i.label;
    }
  }
  // switch (row.type) {
  //   case '10':
  //     cellValue = '轿车';
  //     break;
  //   case '20':
  //     cellValue = '客车';
  //     break;
  //   case '30':
  //     cellValue = '货车';
  //     break;
  //   case '40':
  //     cellValue = '挂车';
  //     break;
  //   default:
  //     cellValue = '未知';
  //     break;
  // }
  return cellValue;
}

// 定义转换电池类型显示为文字的方法
const batteryTypeFormat = (row, column, cellValue, index) => {
  if(!batteryTypeArr.value){
    return cellValue;
  }
  for(let i of batteryTypeArr.value){
    if(i.value==row.batteryType){
      cellValue = i.label;
    }
  }
  // switch (row.batteryType) {
  //   case '10':
  //     cellValue = '铅酸蓄电池';
  //     break;
  //   case '20':
  //     cellValue = '镍氢电池';
  //     break;
  //   case '30':
  //     cellValue = '钠硫电池';
  //     break;
  //   case '40':
  //     cellValue = '二次锂电池';
  //     break;
  //   case '50':
  //     cellValue = '空气电池';
  //     break;
  //   case '60':
  //     cellValue = '三元锂电池';
  //     break;
  //   case '70':
  //     cellValue = '碱性燃料电池';
  //     break;
  //   default:
  //     cellValue = '未知';
  //     break;
  // }
  return cellValue;
}

// 定义转换车辆状态显示为文字的方法
const statusTypeFormat = (row, column, cellValue, index) => {
  switch (row.status) {
    case '1':
      cellValue = '空闲';
      break;
    case '2':
      cellValue = '占用';
      break;
    default:
      cellValue = '未知';
      break;
  }
  return cellValue;
}

// 关闭弹窗方法
const handleClose = () => {
  if (confirm('确定要关闭弹窗吗？')) {
    dialogVisible.value = false;
    saveVehicleForm.value = {};
  }
}

// 定义方法，用于编辑车辆信息
const editVehicle = (id) => {
  dialogTitle.value = '编辑车辆';
  dialogVisible.value = true;
  // 查出当前车辆的数据，回显在弹窗表单中
  axios.get(BASE_URL + '/v1/vehicle/select?id=' + id)
      .then((response) => {
        if (response.data.code == 2000) {
          saveVehicleForm.value = response.data.data[0];
          console.log(saveVehicleForm.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      });
}

// 定义方法，用于删除车辆信息
const deleteVehicle = (id) => {
  if (confirm('您确定要删除车辆信息吗？该操作不可逆！')) {
    axios.delete(BASE_URL + '/v1/vehicle/delete/' + id)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('删除成功！');
            loadVehicle();
          } else {
            ElMessage.error(response.data.msg);
          }
        });
  }
}
</script>